﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ERMap;

namespace PhoneBook.Group
{
    public class PbShare
    {
        public static DataSet list(string phonebookid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select p.phonebookid,p.phonebookname,g.*,s.* ");
            sb.Append(" from b_group g left outer join b_PbShare s on s.groupid = g.groupid and s.phonebookid = @phonebookid ");
            sb.Append(" left outer join b_phonebook p on s.phonebookid = p.phonebookid and p.phonebookid=@phonebookid");
            sb.Append(" where isnull(g.isdel,0) = 0 ");
            sb.Append(" and isnull(s.isdel,0) = 0 ");
            //sb.Append(" and isnull(p.isdel,0) = 0");

            Hashtable keys = new Hashtable();
            keys["phonebookid"] = phonebookid;

            return ERMapHelper.getCustomData(sb.ToString(), keys);
        }

        public static DataSet queryphonebook(string groupid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select distinct p.phonebookid,p.phonebookname from b_PbShare s inner join b_phonebook p on s.phonebookid = p.phonebookid ");
            sb.Append(" where p.groupid = @groupid ");
            sb.Append(" and isnull(p.isdel,0) = 0 and isnull(s.isdel,0) = 0 ");

            Hashtable keys = new Hashtable();
            keys["groupid"] = groupid;

            return ERMapHelper.getCustomData(sb.ToString(), keys);
        }

        public static void cancel(string phonebookid)
        {
            Hashtable keys = new Hashtable();
            keys["phonebookid"] = phonebookid;
            ERMapHelper.destroy("b_PbShare", keys);
        }

        public static void add(string groupid, string phonebookid)
        {
            Hashtable pbshare = new Hashtable();
            pbshare["groupid"] = groupid;
            pbshare["phonebookid"] = phonebookid;
            ERMapHelper.setmodel("b_PbShare", "phonebookid,groupid", pbshare);
        }

        public static void del(string groupid, string phonebookid)
        {
            Hashtable pbshare = new Hashtable();
            pbshare["groupid"] = groupid;
            pbshare["phonebookid"] = phonebookid;
            ERMapHelper.destroy("b_PbShare", pbshare);
        }

        public static DataSet queryshare(string groupid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select g.groupname,p.phonebookid,p.phonebookname,g.phone,g.fax ");
            sb.Append(" from b_PbShare s inner join b_phonebook p on s.phonebookid = p.phonebookid ");
            sb.Append(" inner join b_group g on p.groupid = g.groupid ");
            sb.Append(" where s.groupid = @groupid ");
            sb.Append(" and isnull(p.isdel,0) = 0 and isnull(s.isdel,0) = 0 ");

            Hashtable keys = new Hashtable();
            keys["groupid"] = groupid;

            return ERMapHelper.getCustomData(sb.ToString(), keys);
        }

        public static Hashtable get(string phonebookid, string groupid)
        {
            Hashtable keys = new Hashtable();
            keys.Add("phonebookid", phonebookid);
            keys.Add("groupid", groupid);
            return ERMapHelper.getmodel("b_pbshare", keys);
        }
    }
}
